package com.wectrls.service.impl;

import com.google.common.collect.Lists;
import com.wectrls.config.exceptiom.CommonException;
import com.wectrls.entity.UserGroup;
import com.wectrls.entity.WecsUser;
import com.wectrls.enums.COMMON_STATUS_ENUM;
import com.wectrls.enums.ROLE_ENUM;
import com.wectrls.mapper.UserGroupMapper;
import com.wectrls.mapper.WecsUserMapper;
import com.wectrls.security.SecurityCommonUtils;
import com.wectrls.service.WecsUserService;
import com.wectrls.vo.postvo.WecsUserAddVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author chenjian
 * @Package
 * @Description:
 * @date 2019-10-30 21:56
 */
@Service
@Transactional
public class WecsUserServiceImpl implements WecsUserService, UserDetailsService {
    @Autowired
    private WecsUserMapper wecsUserMapper;

    @Autowired
    private UserGroupMapper userGroupMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;


    /**
     * 新增用户
     *
     * @param wecsUserAddVo
     * @return
     */
    @Override
    public WecsUser addOne(WecsUserAddVo wecsUserAddVo) {
        WecsUser wecsUser = new WecsUser();
        UserGroup userGroup = new UserGroup();
        userGroup.setGroupStatus(COMMON_STATUS_ENUM.PRV.getStatus());
        userGroup.setUsedNum(1);
        wecsUser.setUserGroupId(userGroupMapper.saveAndFlush(userGroup).getId());
        wecsUser.setEmail(wecsUserAddVo.getEmail());
        wecsUser.setPassword(passwordEncoder.encode(wecsUserAddVo.getPassword()));
        wecsUser.setCreateTime(System.currentTimeMillis());
        wecsUser.setRoleName(ROLE_ENUM.GROUP_ADMIN.getRoleName());
        wecsUser.setStatus(COMMON_STATUS_ENUM.NORMAL.getStatus());
        WecsUser saveAndFlush = wecsUserMapper.saveAndFlush(wecsUser);
        return saveAndFlush;
    }

    /**
     * 检查用户的唯一性
     *
     * @param email
     * @return
     */
    @Override
    public List<WecsUser> checkEmail(String email) {
        List<WecsUser> wecsUserList = wecsUserMapper.findByEmail(email);
        return wecsUserList;
    }

    /**
     * 获取用户详情
     *
     * @return
     */
    @Override
    public WecsUser getUserInfo() {
        String user = SecurityCommonUtils.CurrentLoginUser();
        List<WecsUser> wecsUserList = wecsUserMapper.findByEmail(user);
        if (wecsUserList.size() == 1) {
            return wecsUserList.get(0);
        }
        throw new CommonException("查询用户详情失败", 5000);

    }

    /**
     * 获取当前登录用户
     *
     * @return
     */
    @Override
    public WecsUser findBySecurityUserName() {
        List<WecsUser> userList = wecsUserMapper.findByEmail(SecurityCommonUtils.CurrentLoginUser());
        if (userList.size() == 1) {
            WecsUser wecsUser = userList.get(0);
            return wecsUser;
        } else {
            throw new CommonException("非法数据", 5000);
        }
    }

    /**
     * 查询组用户
     *
     * @param groupId
     * @return
     */
    @Override
    public List<WecsUser> findByGroupId(String groupId) {
        List<WecsUser> wecsUserList = wecsUserMapper.findByUserGroupId(groupId);
        return wecsUserList;
    }

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        List<WecsUser> userList = wecsUserMapper.findByEmail(s);
        if (userList.size() == 1) {
            WecsUser wecsUser = userList.get(0);
            List<GrantedAuthority> authList = Lists.newArrayList();
            authList.add(new SimpleGrantedAuthority("ROLE_" + wecsUser.getRoleName()));
            return new User(wecsUser.getEmail(), wecsUser.getPassword(), authList);
        } else {
            throw new CommonException("登录失败", 4001);
        }
    }
}
